package com.mission.schedule.my160920;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.activity.BaseActivity;
import com.mission.schedule.applcation.App;
import com.mission.schedule.my160920.adapter.CommonReplyAdapter;
import com.mission.schedule.my160920.bean.CommonReplyBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liwei on 16/9/20.
 */
public class CommonReplyActivity extends BaseActivity {


    private CommonReplyAdapter commonReplyAdapter;
    private RecyclerView recyclerView;
    private List<CommonReplyBean> commonReplyBeanList;
    private LinearLayout top_ll_left;
    private TextView te;


    @Override
    protected void setListener() {

    }

    @Override
    protected void setAdapter() {

    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.commonreply);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @Override
    protected void init(Bundle savedInstanceState) {


        top_ll_left = (LinearLayout) findViewById(R.id.ly_top_ll_left);
        te = (TextView) findViewById(R.id.te);

        top_ll_left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });


        recyclerView = (RecyclerView) findViewById(R.id.reply_recylerview);

        recyclerView.setHasFixedSize(true);

        commonReplyBeanList = new ArrayList<>();
        commonReplyBeanList = App.getDBcApplication().queryFriendCommonReply();
        commonReplyAdapter = new CommonReplyAdapter(CommonReplyActivity.this, commonReplyBeanList, handler);
        recyclerView.setLayoutManager(new LinearLayoutManager(CommonReplyActivity.this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(commonReplyAdapter);

        commonReplyBeanList = App.getDBcApplication().queryFriendCommonReply();
        if (commonReplyBeanList.size() <= 0) {
            Message message = new Message();
            message.what = 2;
            handler.sendMessage(message);
        }
//        commonReplyAdapter = new CommonReplyAdapter(this, commonReplyBeanList, handler);
//        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
//        recyclerView.setItemAnimator(new DefaultItemAnimator());
//        recyclerView.setAdapter(commonReplyAdapter);


    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    String message = msg.obj + "";
                    Intent intent = new Intent();
                    intent.putExtra("mess", message);
                    setResult(RESULT_OK, intent);
                    finish();
                    break;
                case 2:
                    commonReplyBeanList = App.getDBcApplication().queryFriendCommonReply();
                    if (commonReplyBeanList.size() <= 0) {
                        App.getDBcApplication().insertFriendCommonReply("已收到");
                    }
                    commonReplyAdapter = new CommonReplyAdapter(CommonReplyActivity.this, commonReplyBeanList, handler);
                    recyclerView.setLayoutManager(new LinearLayoutManager(CommonReplyActivity.this));
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    recyclerView.setAdapter(commonReplyAdapter);
                    break;
            }
        }
    };


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }


}
